<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-3-29 0029
 * Time: 22:38
 */

namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use think\Db;
use app\user\model\UserModel;

require_once  '../../dypls/simplewind/extend/dypls/vendor/autoload.php';
use Aliyun\Core\Config;
use Aliyun\Core\Exception\ClientException;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Api\Dypls\Request\V20170525\BindAxbRequest;
use Aliyun\Api\Dypls\Request\V20170525\BindAxnRequest;
use Aliyun\Api\Dypls\Request\V20170525\UnbindSubscriptionRequest;
use Aliyun\Api\Dypls\Request\V20170525\UpdateSubscriptionRequest;
use Aliyun\Api\Dypls\Request\V20170525\QueryRecordFileDownloadUrlRequest;
use Aliyun\Api\Dypls\Request\V20170525\QuerySubscriptionDetailRequest;
use Aliyun\Api\Dypls\Request\V20170525\BindAxnExtensionRequest;

Config::load();


class DyplsController extends HomeBaseController
{
    static $acsClient = null;

    /*stdClass Object
    (
    [Message] => OK
    [RequestId] => 5EBFC606-AB6F-4770-A6CD-7B4BDFA364EB
    [Code] => OK
    [SecretBindDTO] => stdClass Object
    (
    [SecretNo] => 9501364003040
    [SubsId] => 63786
    )

    )*/

    public function bindphone(){

        $no = input('get.no');
        $actin=input('get.action');
        $tem=0;
        if(!empty($no)){
            if($actin=='add'){
                $mobile=input("get.mobile");
                $chepaihao=input("get.chepai");
                $register          = new UserModel();
                $user['user_pass'] =substr($mobile,6,4).$no;
                $user['mobile']=$mobile;
                $log            = $register->registerMobile($user);
                if($log==0 or $log==1){
                    $userid=Db::name('user')->where("mobile=$mobile")->value('id');
                    $data=[
                        'chepaihao'=>$chepaihao,
                        'userid'=>$userid,
                        'ma'=>$no
                    ];
                    Db::name('nuochema')->insert($data);
                    $this->assign('message',"绑定成功");



                }
            }
            else{
                $nuoche=Db::name('nuochema')->where('ma='.$no)->find();
                if($nuoche){
                    $use = Db::name('user')->where("id=$nuoche[userid]")->find();
                    $phone=$use['mobile'];
                    $response = $this->bindAxn($phone);
                    //print_r($response);
                    if($response->Code=="OK"){
                        $tem=3;
                        $this->assign("phone",$response->SecretBindDTO->SecretNo);
                    }
                    else{
                        $mes=$response->Message.",请联系我们客服";
                        $this->assign("message",$mes);
                    }
                }
                else{
                    $this->assign('no',$no);

                    $tem = 2;
                }
            }


        }
        else{
            $tem=1;
        }

        $this->assign('tem',$tem);
        return $this->fetch(':dypls');
    }

    public static function getAcsClient() {
        //产品名称:云通信流量服务API产品,开发者无需替换
        $product = "Dyplsapi";

        //产品域名,开发者无需替换
        $domain = "dyplsapi.aliyuncs.com";

        // TODO 此处需要替换成开发者自己的AK (https://ak-console.aliyun.com/)
        $accessKeyId = "LTAI0sMJiIYhEAZL"; // AccessKeyId

        $accessKeySecret = "wQpw6Fk8v9SmiFYAgpttm69C1WNyoC"; // AccessKeySecret


        // 暂时不支持多Region
        $region = "cn-hangzhou";

        // 服务结点
        $endPointName = "cn-hangzhou";


        if(static::$acsClient == null) {

            //初始化acsClient,暂不支持region化
            $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);

            // 增加服务结点
            DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);

            // 初始化AcsClient用于发起请求
            static::$acsClient = new DefaultAcsClient($profile);
        }
        return static::$acsClient;
    }

    function bindAxn($mobile){
        //组装请求对象-具体描述见控制台-文档部分内容
        $request = new BindAxnRequest();
        //可选-启用https协议
        //$request->setProtocol("https");

        //必填:号池Key
        $request->setPoolKey("FC100000029464051");
        //必填:AXB关系中的A号码
        $request->setPhoneNoA($mobile);

        //可选:AXN中A拨打X的时候转接到的默认的B号码,如果不需要则不设置
        $request->setPhoneNoB("13838396271");

        //可选:指定X号码进行选号
        // $request->setPhoneNoX("17000000000");

        //可选:期望分配X号码归属的地市(省去地市后缀后的城市名称)
        $request->setExpectCity("北京");

        //必填:95中间号,NO_170代表选择使用170号码资源
        $request->setNoType("NO_95");

        //必填:绑定关系对应的失效时间-不能早于当前系统时间

        $request->setExpiration(date("Y-m-d H:i:s",time()+180));

        //可选:是否需要录制音频-默认是false
        $request->setIsRecordingEnabled(false);

        //可选:外部业务自定义ID属性
        $request->setOutId("yourOutId");

        //hint 此处可能会抛出异常，注意catch
        $response = static::getAcsClient()->getAcsResponse($request);

        return $response ;
    }


}